//
//  ST_NextST_InteractivePairsCenterController.swift
//  btvideo
//
//  Created by mac on 2020/5/19.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SwifterSwift
import HandyJSON
import SnapKit
import SwiftyUserDefaults
import MJRefresh


/**
 可交互的 用户个人中心使用
 */
class ST_NextST_InteractivePairsCenterController: ST_PairsCenterViewController {
    
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate var data_adapter:ST_InteractivePairsProtocol?
    
    convenience init(album:[ST_PairsShareParticularModel],userId:String,personSign:String,nickName:String,data_adapter:ST_InteractivePairsProtocol?) {
        
        self.init(nibName: nil, bundle: nil)
        
        self.data_adapter = data_adapter

        self.accid = userId
        
        self.viewModel.tableHeaderViewModel.particularArray = album
        
        self.viewModel.tableHeaderViewModel.accid = userId
        
        self.viewModel.tableHeaderViewModel.nickname = nickName
        
        self.viewModel.tableHeaderViewModel.sign = personSign
        
        self.viewModel.tableHeaderViewModel.setUpViewDataSet()
    }
    
//    MARK: - override Method
//    override func ignore_next_step() {
//
//       if home_pushed{
//
//           self.push_to_next_user(matchSuccess: false)
//
//           NotificationCenter.default.post(name: Notification.Name.ignore_somebody, object: nil, userInfo: ["accid": self.accid])
//
//       }else{
//
//           super.ignore_next_step()
//       }
//
//   }
    override func operate_after_handle(){
         
         self.layout_operation_btns()
         
        self.push_to_next_user(matchSuccess: false, removeOper: false)
     }
     
     override func ignore_next_step(){
            
            
        self.layout_operation_btns()
        
        self.push_to_next_user(matchSuccess: false, removeOper: true)
         
         NotificationCenter.default.post(name: Notification.Name.ignore_somebody, object: nil, userInfo: ["accid": self.accid])
    }
    
    fileprivate func push_to_next_user(matchSuccess:Bool,removeOper:Bool){
                
        guard let next = self.data_adapter?.fetch_next_user(remove: removeOper) else{

//            最后一个 如果是 匹配成功 不关闭当前界面
            if !matchSuccess{
                back_handle()
            }
            
           return
       }
        
        let vc = ST_NextST_InteractivePairsCenterController.init(album: next.resourceList, userId: next.accid,personSign: next.sign, nickName: next.nickname, data_adapter: self.data_adapter)
        
        self.navigationController?.pushViewController(vc)
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        
        if let first = viewModel.tableHeaderViewModel.particularArray.first{
            
            self.header_height = first.displayHeight + viewModel.tableHeaderViewModel.headerExtensionHeight
            
        }
        
        self.headerView.bind_model(self.viewModel.tableHeaderViewModel)
        
    }
    
//    MARK: - override Proeprties
    private lazy var _headerView:ST_PairsCenterHeaderView = {
        
        let control = ST_PairsCenterHeaderView(frame: CGRect.init(x: 0, y: 0, width: ScreenWidth, height: header_height))
        
        control.phDelegate = self
        
        control.create_play_view()
        
        return control
    }()
    
    override var headerView: ST_PairsCenterHeaderView{
        get { return _headerView }
        set { _headerView = newValue }
    }
//    MARK: - Fetch Data
    override func fetch_remote_data_set() {
        
        print("个人主页接口开始",Date())
        
        JVrequest(PH.otherPeopleNewInfoApi,parameters: ["target_accid":accid],success: { (json) in
            
            print("个人主页接口结束",Date())
            
//            print("新其他人 数据集:",json)
            
            self.viewModel = ST_PairsUserCenterViewModel.deserialize(from: json["data"], accid: self.accid)
            /*
            guard let pairsModel = ST_PairsUserModel.deserialize(from: json["data"].rawString() ?? "" ) else{
                print("数据结构不对")
                return
            }
            
            self.viewModel.datasets.removeAll()
            
            pairsModel.paddingOtherPeopleData(viewModel: self.viewModel, informationJson: json["data"]["information"])
             
            */
            
            self.viewModel.tableHeaderViewModel.accid = self.accid
            
            self.viewModel.tableHeaderViewModel.setUpViewDataSet()
            
            self.headerView.bind_model(self.viewModel.tableHeaderViewModel)
            
            self.tableView.reloadData {
                
                self.tableView.mj_footer = MJRefreshAutoNormalFooter.init(refreshingBlock: {[weak self] in
                    
                    self?.request_recommend_data()
                })
                
                self.request_match_data()
            }
            
            self.layout_operation_btns()
            
            
        }) { (code, error) in
            print("当前错误❌:",error)
        }
        
        
    }
    
    
    
}




